import { api_users_checkLogin } from "@/apis/user"

const state = {
    token: localStorage.getItem('token'),
    role: localStorage.getItem('role')
}

const mutations = {
    // 存token
    SET_TOKEN(state, data) {
        state.token = data
        localStorage.setItem('token', data)
    },
    // 存role
    SET_ROLE(state, data) {
        state.role = data
        localStorage.setItem('role', data)
    },
}

const actions = {
    getLogin({ commit }, data) {
        return new Promise(async resolve => {
            const res = await api_users_checkLogin(data)
            let { code, msg, role, token } = res.data
            if (code === 0) {
                commit('SET_TOKEN', 'Bearer ' + token)
                commit('SET_ROLE', role)
            }
            resolve()
        })
    }
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}